$(document).ready(function (){
require(["inputdrop",'tree'] ,function(inputdrop,tree){


var index_list_url = "knowledge/secEventType/queryEventList";

var index_list_col_header = [
							  {name:"",text:"" ,hideSearch:"hide",width:10},
							  {text : "事件名称" ,name : "eventName",align:"left",width:31},
							  {text : "事件描述" ,name : "eventDesc",align:"left",width:59,hideSearch:true}
							];

var index_list_col_oper = [
				  			  {icon : "rh-icon rh-edit" ,text : "修改" ,aclick :edit_template_init}
			   			  ];

var list_add_url = "knowledge/secEventType/addEvent";

var list_update_url = "knowledge/secEventType/updEvent";

var eventTypeTree_url = "knowledge/secEventCtg/queryEventTypeCodeList";

var el_table_div = $("#table_div1");

var current_selected_node_id = -1;

var current_selected_node_name = "事件类型";

event_init(); 

event_class_tree();

index_list_get({paramObj : null ,isLoad : false ,maskObj : "body"});

function event_init()
{
	$("#add_btn").click(function (){
		edit_template_init();
	});
}

function event_class_tree(opt)
{
	if(opt)
	{
		current_selected_node_id = opt;
	}
	um_ajax_get({
		url : eventTypeTree_url,
		successCallBack : function (data){
			for (var i = 0; i < data.length; i++)
			{
				data[i].parent = "-1";
			}
			data.push({codevalue:"-1" ,parent:"root" ,codename:"事件类型"});
			tree.render($("#event_class_tree") ,{
				pId:"parent",
				label:"codename",
				id:"codevalue",
				zNodes : data,
				expandNode : -1,
				selectNode : current_selected_node_id,
				zTreeOnClick : function (event, treeId, treeNode){
					current_selected_node_id = treeNode.id;
					current_selected_node_name = treeNode.label;
					var paramObj = {eventTypeId:treeNode.id};
					if(treeNode.id == "-1")
					{
						paramObj = null;
					}
					index_list_get({
						paramObj:paramObj,
						isLoad:true,
						maskObj:"body"
					});
				}
			});
		}
	});
}
function index_list_get(option)
{
	g_grid.render($("#table_div1") ,{
 		header : index_list_col_header,
 		oper : index_list_col_oper,
 		operWidth : "60px",
 		url : index_list_url,
 		paramObj : option.paramObj,
 		queryBefore:function(paramObj){
 			paramObj.eventTypeId = current_selected_node_id;
 		},
 		isLoad : option.isLoad,
 		maskObj : option.maskObj,
 		allowCheckBox:false,
 		dbIndex:1,
 		dbClick : detail_template_init,
 		hasBorder : false,
	    autoHeight : true,
	    showCount : true
	});
}


function edit_template_init(rowData)
{
	var target_title = rowData ? "修改事件" : "添加事件";

	$.ajax({
		type: "GET",
		url: "module/base_resource_mant/sec_event_manage/sec_event_info_repo_tpl.html",
		success : function(data){
			g_dialog.dialog($(data).find("[id=list_edit_template]") ,{
				width : "500px",
				init : init,
				title : target_title,
				saveclick : save_click
			});
		}
	});

	function init(el)
	{
		g_validate.init(el);
		um_ajax_get({
			url:eventTypeTree_url,
			isLoad:true,
			maskObj:"body",
			successCallBack:function(data){
				for (var i = 0; i < data.length; i++) {
	    			data[i].id = data[i].codevalue;
	    			data[i].text = data[i].codename;
	    		}
	    		el.find("[data-id=eventTypeId]").select2({
	    			data : data,
	    			width : "100%"
	    		});
	    		if (rowData) {
					el.umDataBind("render",rowData);
					el.find("[data-id=eventTypeId]").attr("disabled","disabled");
					el.find("select").trigger("change");
				}
			}
		});

	}
	function save_click(el ,saveObj)
	{
		if (!g_validate.validate(el)){
			return false;
		}
		var url = list_add_url;
		if (rowData) 
		{
			url = list_update_url;
			saveObj.eventTypeId = rowData.eventTypeId;
			saveObj.eventId = rowData.eventId;
		}
		current_selected_node_id = saveObj.eventTypeId;
		um_ajax_post({
			url:url,
			paramObj:saveObj,
			isLoad:true,
			maskObj:"body",
			successCallBack:function(data){
				g_dialog.hide(el);
				g_dialog.operateAlert();
				var paramObj = {eventTypeId:current_selected_node_id};
				index_list_get({
					paramObj:paramObj,
					isLoad:false,
				});
				event_class_tree(current_selected_node_id);
			}
		});
		
	}
}

function detail_template_init(rowData)
{

	$.ajax({
		type: "GET",
		url: "module/base_resource_mant/sec_event_manage/sec_event_info_repo_tpl.html",
		success :function(data)
		{
			g_dialog.dialog($(data).find("[id=list_detail_template]") ,{
				width : "500px",
				title : "事件详细信息",
				init : init,
				isDetail : true
			});
			function init(el)
			{
				el.umDataBind("render" ,rowData);
			}
		}
	});
}


});
});